feat: add comparison operators <> <= >= and BEGIN/WHILE/REPEAT loop#15
feat: add comparison operators <> <= >= and BEGIN/WHILE/REPEAT loop#15tetsuo-cpp merged 3 commits intocanonfrom
Conversation
tetsuo-cpp
left a comment
There was a problem hiding this comment.
PR #15 Review: feat: add comparison operators <> <= >= and BEGIN/WHILE/REPEAT loop
Overview
This PR adds two features:
- Comparison operators
<>(ne),<=(le),>=(ge) — straightforward additions following existing patterns BEGIN/WHILE/REPEATpre-test loop — a new two-region op that lowers toscf.while
Code Quality & Correctness
Comparison operators — Clean and correct. Uses the existing BinaryCmpOpConversion template with the right arith::CmpIPredicate variants (ne, sle, sge). Signed predicates are consistent with the existing slt/sgt usage.
BEGIN/WHILE/REPEAT lowering — Well-structured. The two-region approach (condition + body) maps naturally to scf.while's before/after regions. The conversion pattern correctly follows the established convertRegionTypes + inlineRegionBefore + mergeBlocks pattern from BeginUntilOpConversion.
YieldOp while_cond attribute — Clever reuse. Instead of creating a separate yield op, the UnitAttr flag distinguishes WHILE (continue on non-zero) from UNTIL (exit on non-zero) semantics in a single conversion pattern.
Issues
-
isWhileLoopdoesn't trackIF/THENnesting (ForthToMLIR.cpp:547). It only increments depth onBEGIN/DOand decrements onUNTIL/LOOP/REPEAT. AWHILEword used inside anIF/ELSE/THENblock within aBEGIN/UNTILloop would be incorrectly treated as the loop's ownWHILE. In practice this is unlikely since standard Forth doesn't nestWHILEinsideIFwithinBEGIN, so this is probably fine for real-world code — just worth noting. -
YieldOpConversioncomment is now stale (ForthToMemRef.cpp:626-627). The doc comment still says "Context-aware: inside scf.while'sbeforeregion (from BeginUntilOp)" — it should mentionBeginWhileRepeatOpas well.
Suggestions
-
Minor: The
getSuccessorRegionsimplementation inForthDialect.cpplooks correct for the three cases (parent → condition, condition → body/exit, body → condition). Well done matching thescf.whilesemantics. -
Consider: Adding a nested loop test case, e.g.
BEGIN ... BEGIN ... UNTIL ... WHILE ... REPEATto verify the lookahead handles nesting correctly.
Test Coverage
Good coverage across all three test tiers:
- Translation tests verify parsing (
begin-while-repeat.forth,comparison-ops.forth) - Conversion tests verify MemRef lowering (
begin-while-repeat.mlir,comparison.mlir) - Pipeline test verifies full compilation to GPU binary (
begin-while-repeat.forth)
Summary
Solid PR. The comparison operators are trivially correct. The BEGIN/WHILE/REPEAT implementation is well-designed — reusing YieldOp with a while_cond attribute is a clean approach that avoids duplicating the yield conversion logic. The two substantive notes are the stale comment and the IF/THEN-nesting edge case in the lookahead (low risk in practice). Looks good to merge after addressing the comment.
8558561 to
5fc00cc
Compare
Summary
<>(not-equal),<=(less-or-equal),>=(greater-or-equal)BEGIN/WHILE/REPEATpre-test loop as a two-region op that lowers toscf.whileCLAUDE.mdsupported words listTest plan
comparison-ops.forthtranslation test with<>,<=,>=comparison.mlirconversion test withne,le,gepredicatesbegin-while-repeat.forthtranslation testbegin-while-repeat.mlirconversion testbegin-while-repeat.forthfull pipeline testCloses #9